package com.gdth.widget.dialog;

import com.gdth.R;

import android.content.Context;
import android.os.Looper;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class BaseToast {
	
	private static BaseToast instance;
	
	public static BaseToast getInstance(){
		if (instance == null)
			instance = new BaseToast();
		
		return instance;
	}
	
	private Toast mToast;
	private Toast centerToast;
	private Toast imageToast;
	private Toast myToast;
	
	public BaseToast(){
		
	}
	
	public void ShowSystemToast(Context mContext, String value){
		try {
			
			if (mToast == null)
				mToast = Toast.makeText(mContext.getApplicationContext(), value, Toast.LENGTH_SHORT);
			
			else 
				mToast.setText(value);
			
			mToast.show();
		}catch(IllegalStateException e) {
			
		}catch(Exception e) {
			
		} 
		
	}
	
	public void ShowCenterToast(Context mContext, String value) throws IllegalStateException{
		try {
			if (centerToast == null){
				centerToast = Toast.makeText(mContext.getApplicationContext(), value, Toast.LENGTH_LONG);
				centerToast.setGravity(Gravity.CENTER, 0, 0);
			}
			else 
				centerToast.setText(value);
			centerToast.show();
			
		}catch(IllegalStateException e) {
			
		 } catch (Exception e) {
			  
		 }
		
	}

	public void ShowImageToast(Context mContext, String value){
		try {
			if (imageToast == null){
				imageToast = Toast.makeText(mContext.getApplicationContext(), value, Toast.LENGTH_LONG);
				imageToast.setGravity(Gravity.CENTER, 0, 0);
				LinearLayout toastView = (LinearLayout) imageToast.getView();
				ImageView imageCodeProject = new ImageView(mContext.getApplicationContext());
				imageCodeProject.setImageResource(R.drawable.listview_item_img);
				toastView.addView(imageCodeProject, 0);
			}
			else {
				imageToast.setText(value);
			}
			
			imageToast.show();
		}catch(IllegalStateException e) {
		
		} catch (Exception e) {
		  
		}
	}

	public void showinitToast(Context mContext, String value){
		try {
			if (myToast == null){
				LayoutInflater inflater = (LayoutInflater) LayoutInflater.from(mContext);
				int layouts = R.layout.layout_toast;
				View layout = inflater.inflate(layouts, null).findViewById(R.id.llToast);
				ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);
				image.setImageResource(R.drawable.listview_item_img);
				TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
				title.setText("Attention");
				TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
				text.setText(value);
				myToast = new Toast(mContext.getApplicationContext());
				myToast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
				myToast.setDuration(Toast.LENGTH_LONG);
				myToast.setView(layout);
			}
			else{
				myToast.setText(value);
			}
			myToast.show();
		}catch(IllegalStateException e) {
			
		} catch (Exception e) {
		  
		}
	}
}
